//
//  AppDelegate.swift
//  YFSwiftWeibo
//
//  Created by tsaievan on 2017/8/24.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var _application: UIApplication?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        _application = application
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = YFMainController()
        window?.makeKeyAndVisible()
        loadVcInfo()
        
        ///< 获取用户接受通知的权限
        requestNofify()
        ///< 设置额外的一些信息
        setAdditions()
        return true
    }
}


extension AppDelegate {
    fileprivate func loadVcInfo() {
        
        // 模拟异步加载数据
        DispatchQueue.global().async { 
            guard let path = Bundle.main.path(forResource: "main.json", ofType: nil),
            let jsonData = NSData(contentsOfFile: path) else {
                return
            }
            let jsonPath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("main.json")
            jsonData.write(toFile: jsonPath, atomically: true)
            print("保存数据成功\(jsonPath)")
        }
    }
}

extension AppDelegate {
    fileprivate func requestNofify() {
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .carPlay, .sound]) { (isSuccess, error) in
                print("")
            }
        } else {
            // ios10以下, 取得用户授权接受通知的方法:
            let notifySettings = UIUserNotificationSettings(types: [.alert, .sound, .badge], categories: nil)
            _application?.registerUserNotificationSettings(notifySettings)
        }
    }
}

extension AppDelegate {
    fileprivate func setAdditions() {
        ///< 设置蒙版最小显示时间
        SVProgressHUD.setMinimumDismissTimeInterval(2)
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
    }
}
